# -*- coding:utf-8 -*-
from database.db_handler import MysqlHander
from database.db_business import DbBusiness
from common.my_http import MyHttp
from common.my_baidu import MyBaidu
from common.my_time import MyTime
from common.my_file import MyFile
from common.macro_data import MacroData
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import os


'''
网络数据
'''

class BaiduWaiguozhengce:
    def __init__(self):
        self.macro = MacroData()
        self.business = DbBusiness()
        self.log_file = open('./out/' + os.path.split(__file__)[-1].split(".")[0] + ".log", 'a', encoding = 'utf-8')
        MyFile.wrtie_log(self.log_file, "开始")
        self.all = 0
        self.error = 0

    def __del__(self):
        MyFile.wrtie_log(self.log_file, "all:" + str(self.all) + ",error:" + str(self.error))
        MyFile.wrtie_log(self.log_file, "结束")
        self.log_file.close()

    def check_title(data_title):
        deny_list = ["前总统","前首相","前总理","前主席"]
        for d in deny_list:
            if re.search(d, data_title):
                return False
        return True

    def get_data(self, words, keys):
        end_date = MyTime.forward_relative_date(30)
        next_page = True
        for i in range(1,6):
            #time.sleep(1)
            if next_page == False:
                break
            url = MyBaidu.get_url_order_by_time(words, i)
            try:
                proxy = self.business.query_proxy()
                res = MyBaidu.get_baidu_data_by_proxy(url, proxy)
                self.all = self.all + 1
                for r in res:
                    data_date = MyBaidu.calc_date(r["date"])
                    if data_date < end_date:
                        next_page = False
                        break
                    #if MyBaidu.check_site(r["site"]) == False:
                    #    continue
                    if self.check_title(r["title"]) == False:
                        continue
                    if MyBaidu.check_title(r["title"], keys, ["?",";","-"]) == False:
                        continue
                    data_title = r["title"]
                    data_site = r["site"]
                    data_url = r["url"]
                    print(r["date"] + data_date)
                    self.macro.add_macro_data(data_date, 299, data_title, data_url, data_site)
            except Exception as e:
                self.error = self.error + 1
                #i = i - 1
                pass

def waiguozhengce_api():
    d = BaiduWaiguozhengce()
    d.get_data(["欧洲议会"], [["欧洲议会"],["投票","通过"]])
    diqu = d.business.query_gdp80()
    for data in diqu:
        if data == "中国" or data == "美国":
            continue
        d.get_data([data, "众议院"], [[data],["众议院"]])
        d.get_data([data, "参议院"], [[data],["参议院"]])
        d.get_data([data, "总理"], [[data],["总理"]])
        d.get_data([data, "总统"], [[data],["总统"]])
        d.get_data([data, "国家主席"], [[data],["国家主席"]])
        d.get_data([data, "首相"], [[data],["首相"]])
        d.get_data([data, "政府"], [[data],["政府"]])

if __name__ == '__main__':
    waiguozhengce_api()